package com.Dao.Imp;
import com.Dao.FindBookDao;
import com.Utils.C3P0Utils;
import com.pojo.FindBook;
import com.pojo.FindRemark;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.BeanMapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
@Service
@Slf4j
public class FindBookDaoImp implements FindBookDao {
    @Override
    public List<FindBook> findBooksByAuthor(String key, String flag)  {
        FindBook findBook = new FindBook();
        findBook.setFlag(flag);
        System.out.println(findBook.getFlag());
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select id,bookName,author,mainsignature,signature2,signature3,readQuantity,snopsis,bookImage,recommendQuantity from books_detail " +
                "where author like ? or bookName like ? or mainsignature like ?";
        String keyWord = "%" + key + "%";
        List<FindBook> bookList = null;
        try {
            bookList = queryRunner.query(sql, new BeanListHandler<>(FindBook.class), keyWord,keyWord,keyWord);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(flag.equals("submit")||flag.equals("submit1")){
            return bookList;
        }else if(flag.equals("sortByHot")){
            Collections.sort(bookList);
            Collections.reverse(bookList);
        }else{
            Collections.sort(bookList);
            Collections.reverse(bookList);
        }
        return bookList;
    }
    @Override
    public List<FindBook> findAllBooks()  {

        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select id,bookName,author,profile,score,mainsignature,signature2,signature3,readQuantity,snopsis,bookImage,recommendQuantity from books_detail ";
        List<FindBook> bookList = null;
        try {
            bookList = queryRunner.query(sql, new BeanListHandler<>(FindBook.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return bookList;
    }
    @Override
    public List<FindBook> findByTypeAndLimit(String type, Integer limit) {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select id,bookName,author,profile,score,mainsignature,signature2,signature3,readQuantity,snopsis,bookImage,recommendQuantity from books_detail " +
                "where mainsignature = ? ORDER BY RAND() limit ?";
        List<FindBook> bookList = null;
        try {
            bookList = queryRunner.query(sql, new BeanListHandler<>(FindBook.class),type,limit);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return bookList;
    }
    @Override
    public FindBook findBookById(String bookId)  {

        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select id,bookName,author,profile,score,background,mainsignature,signature2,signature3,signature4,signature5,wordage,readQuantity,addQuantity,snopsis,bookImage,recommendQuantity from books_detail " +
                "where id = ?";
        FindBook findBook =null;
        try {
            findBook = queryRunner.query(sql, new BeanHandler<>(FindBook.class),bookId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return findBook;
    }
    @Override
    public List<FindRemark> findBookCommentPage(String bookId, Integer pageNum, Integer pageSize,String time) {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select r.*,u.nickName,u.userAvatar from remark r left join userlist u on r.userId = u.id where bookId = ? ORDER BY r.remarkTime DESC limit ?,?";
        List<FindRemark> remarkList = null;
        try {
            remarkList = queryRunner.query(sql, new BeanListHandler<>(FindRemark.class),bookId,(pageNum-1)*3,pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return remarkList;
    }

    @Override
    public Long findBookCommentCount(String bookId) {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

        String sql = "select count(*) from remark where bookId = ?";
        try {
            Long count = queryRunner.query(sql, new ScalarHandler<>(), bookId);
            return count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
